home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / npp / npview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  5.8 KB  |  229 lines

  1. // npview.cpp : implementation of the CNotepadView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "np.h"
  6. #include "mainfrm.h"
  7. #include "npdoc.h"
  8. #include "npview.h"
  9. #include "gotodlg.h"
  10. #if (_WIN32_WCE <= 200)
  11. // This sample had its own find dialog under H/PC 2.0.  Newer OS's have an 
  12. // improved find dialog box and this sample uses it instead.
  13. #include "finddlg.h"
  14. #endif (_WIN32_WCE <= 200)
  15. #include <afxcoll.h>
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CNotepadView
  24.  
  25. IMPLEMENT_DYNCREATE(CNotepadView, CEditView)
  26.  
  27. BEGIN_MESSAGE_MAP(CNotepadView, CEditView)
  28.     //{{AFX_MSG_MAP(CNotepadView)
  29.     ON_COMMAND(ID_EDIT_GOTO, OnEditGoto)
  30.     ON_UPDATE_COMMAND_UI(ID_EDIT_GOTO, OnUpdateEditGoto)
  31. #if (_WIN32_WCE <= 200)
  32.     ON_COMMAND(ID_EDIT_FIND, OnEditFind)
  33.     ON_UPDATE_COMMAND_UI(ID_EDIT_FIND, OnUpdateEditFind)
  34.     ON_COMMAND(ID_VIEW_FIND_NEXT, OnViewFindNext)
  35. #endif (_WIN32_WCE <= 200)
  36.     ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  37.     ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  38.     //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CNotepadView construction/destruction
  43.  
  44. CNotepadView::CNotepadView()
  45. {
  46. #if (_WIN32_WCE <= 200)
  47.     m_pFindDialog = new CFindDlg;        
  48. #endif (_WIN32_WCE <= 200)
  49.     m_pGotoDialog = new CGotoDlg;
  50. }
  51.  
  52. CNotepadView::~CNotepadView()
  53. {
  54.     delete     m_pGotoDialog;
  55. #if (_WIN32_WCE <= 200)
  56.     delete    m_pFindDialog;        
  57. #endif (_WIN32_WCE <= 200)
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CNotepadView drawing
  62.  
  63. void CNotepadView::OnDraw(CDC* pDC)
  64. {
  65.     CNotepadDoc* pDoc = GetDocument();
  66.     ASSERT_VALID(pDoc);
  67. }
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CNotepadView diagnostics
  71.  
  72. #ifdef _DEBUG
  73. void CNotepadView::AssertValid() const
  74. {
  75.     CEditView::AssertValid();
  76. }
  77.  
  78. void CNotepadView::Dump(CDumpContext& dc) const
  79. {
  80.     CEditView::Dump(dc);
  81. }
  82.  
  83. CNotepadDoc* CNotepadView::GetDocument() // non-debug version is inline
  84. {
  85.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNotepadDoc)));
  86.     return (CNotepadDoc*)m_pDocument;
  87. }
  88. #endif //_DEBUG
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CNotepadView message handlers
  92.  
  93. void CNotepadView::OnEditGoto() 
  94. {
  95.     if(m_pGotoDialog->DoModal() != IDOK)
  96.         return;
  97.  
  98.     if(m_pGotoDialog->m_nLineNum < 1)
  99.     {
  100.         CString s1;
  101.         s1.LoadString(NPERROR_INVALID_LINENUM);
  102.         AfxMessageBox(s1);
  103.         return;
  104.     }
  105.  
  106.     CEdit &edit = GetEditCtrl();
  107.     int i = edit.LineFromChar();            // this is the current line the cursor is on
  108.     int nLine = m_pGotoDialog->m_nLineNum;    // line number to go to
  109.  
  110.     if(nLine > (edit.GetLineCount()))
  111.     {
  112.         CString s1;
  113.         
  114.         s1.LoadString(NPERROR_NUM_TOO_BIG);
  115.         AfxMessageBox(s1);
  116.         return;
  117.     }
  118.  
  119.     // move window and caret
  120.     --nLine;                    // edit control is zero based
  121.     edit.LineScroll(nLine-i);    // new line number - the current line
  122.  
  123.     int idx;
  124.     idx = edit.LineIndex(nLine);
  125.     edit.SetSel(idx, idx);
  126. }
  127.  
  128. void CNotepadView::OnUpdateEditGoto(CCmdUI* pCmdUI) 
  129. {
  130.     pCmdUI->Enable(GetWindowTextLength());
  131. }
  132.  
  133. #if (_WIN32_WCE <= 200)
  134. void CNotepadView::OnEditFind() 
  135. {
  136.     CString s1, s2;
  137.  
  138.     // add user selected text from CEditView to the buffer
  139.     GetSelectedText(s1);
  140.     s2 = m_pFindDialog->m_szText; // save old string if user cancels
  141.  
  142.     if(s1.GetLength()) 
  143.         m_pFindDialog->m_szText = s1;
  144.  
  145.     if(m_pFindDialog->DoModal() != IDOK)
  146.     {
  147.         m_pFindDialog->m_szText = s2; // restore old string.  user selected cancel
  148.         return;
  149.     }
  150.     
  151.       ASSERT(m_pFindDialog->m_szText.GetLength());
  152.  
  153.     // add it to the combo control on the toolbar
  154.     if(m_pFindDialog->m_szText.GetLength())
  155.         OnFindNext(m_pFindDialog->m_szText, m_pFindDialog->m_nDirection, m_pFindDialog->m_bMatchCase);
  156. }
  157.  
  158. void CNotepadView::OnUpdateEditFind(CCmdUI* pCmdUI) 
  159. {
  160.     pCmdUI->Enable(GetWindowTextLength());
  161. }
  162.  
  163. void CNotepadView::OnViewFindNext(CString &s1)
  164. {
  165.     OnFindNext(s1, m_pFindDialog->m_nDirection, m_pFindDialog->m_bMatchCase);
  166. }
  167.  
  168. void CNotepadView::OnViewFindNext() 
  169. {
  170.     CString s1;
  171.  
  172.     // if the user presses F3, see if there is a search pattern. if not, ask for one
  173.     if (m_pFindDialog->m_szText.GetLength())
  174.         OnFindNext(m_pFindDialog->m_szText, m_pFindDialog->m_nDirection, m_pFindDialog->m_bMatchCase);
  175.     else 
  176.         PostMessage(WM_COMMAND, ID_EDIT_FIND);
  177. }
  178. #endif (_WIN32_WCE <= 200)
  179.  
  180. BOOL CNotepadView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  181. {
  182.     return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  183. }
  184.  
  185. void CNotepadView::OnEditPaste() 
  186. {
  187.     CWnd* pFocusWnd;
  188.  
  189.     // Note: this is the handler for the accelerator (CTRL+V).  Since the accelerator goes to the
  190.     // frame, find who has the focus to determine where to paste to.
  191.  
  192.     pFocusWnd = GetFocus();
  193. #if defined(_WIN32_WCE_EMULATION)
  194.     if(pFocusWnd == NULL)
  195.         pFocusWnd = GetActiveWindow();
  196. #endif
  197.     ASSERT(pFocusWnd);
  198.  
  199.     GetEditCtrl().Paste();
  200. }
  201.  
  202. void CNotepadView::OnEditCopy() 
  203. {
  204.     CWnd* pFocusWnd;
  205.     // Note: this is the handler for the accelerator (CTRL+C).  Since the accelerator goes to the
  206.     // frame, find who has the focus to determine where to copy from.
  207.  
  208.     pFocusWnd = GetFocus();
  209. #if defined(_WIN32_WCE_EMULATION)
  210.     if(pFocusWnd == NULL)
  211.         pFocusWnd = GetActiveWindow();
  212. #endif
  213.     ASSERT(pFocusWnd);
  214.  
  215.     // if the focus is in the combobox edit control, do the copy from the control
  216.     GetEditCtrl().Copy();
  217. }
  218.  
  219. #if !defined(_WIN32_WCE_PSPC)
  220. BOOL CNotepadView::OnPreparePrinting(CPrintInfo* pInfo)
  221. {
  222.     // Disable margins
  223.     pInfo->m_pPD->m_pd.dwFlags &= ~PD_MARGINS;
  224.     pInfo->m_pPD->m_pd.dwFlags |= PD_DISABLEMARGINS | PD_DISABLEPRINTRANGE;
  225.  
  226.     return CEditView::OnPreparePrinting(pInfo);
  227. }
  228. #endif
  229.